package com.ynr.dict.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.ynr.dao.ICompanyDao;
import com.ynr.voucher.dao.IDictionaryDao;
import com.ynr.voucher.model.FaModel;
import com.ynr.voucher.service.IVoucherService;

@Controller
public class DictController {
		
	private static final Logger logger = LoggerFactory.getLogger(DictController.class);
	
	@Autowired
	private IDictionaryDao dictDao;
	
	@Autowired
	private IVoucherService voucherService;
	
	@Autowired
	private ICompanyDao companyDao;
	
	@RequestMapping("/updateDict")
	@ResponseBody
	public Map<String, Object> updateDict(HttpServletRequest request) {
		Map<String, Object> retMap = new HashMap<>();
		String companyId = request.getParameter("companyId");
		String billDate = request.getParameter("billDate");
		String subject = request.getParameter("subject");
		String dict = request.getParameter("dict");
		String data = request.getParameter("data");
		Map<String, Object> dataMap = JSON.parseObject(data);
		Map<String, Object> companyMap = companyDao.getCompanyById(Long.valueOf(companyId));
		logger.debug("companyId : {}, billDate: {}, subject : {}, dict :{}", companyId, billDate, subject, dict);
		if(subject.startsWith("1601")) {
			String[] subjectType = subject.split("-");
			String faType = subjectType[1];
			dictDao.addFaDict(dict, faType, companyId);
			FaModel fa = dictDao.getFaDepreciationType(faType);
			voucherService.updateFaVoucher(dict, dataMap, fa, companyMap, billDate);
		} else {
			voucherService.updateVoucher(dict, dataMap, subject);
		}
		return retMap;
	}
}
